package com.example.frank.test2.util;

import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.provider.Settings;

import java.util.List;

/**
 * Created by Fadi.Su on 2017/10/25.
 */
public class Utils {

    /**
     * 有权查看使用情况的应用程序
     *
     * @param mContext
     * @return
     */
    public static boolean isNoOption(Context mContext) {
        PackageManager packageManager = mContext.getApplicationContext().getPackageManager();
        Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }

    /**
     * "有权查看使用情况的应用程序"选项是否被打开
     *
     * @return
     */
    public static boolean isNoSwitch(Context mContext) {
        long ts = System.currentTimeMillis();
        UsageStatsManager usageStatsManager = (UsageStatsManager) mContext.getApplicationContext().getSystemService(Context.USAGE_STATS_SERVICE);
        List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(
                UsageStatsManager.INTERVAL_BEST, 0, ts);
        if (queryUsageStats == null || queryUsageStats.isEmpty()) {
            return false;
        }
        return true;
    }

    public static void startSettings(Context mContext) {
        Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
        mContext.startActivity(intent);
    }
}
